<template>
  <div class="app-container">
    <el-tabs v-model="activeName" type="card" @tab-click="handleTabClick">
      <el-tab-pane label="待办列表" name="first">
        <el-card v-show="showSearch" style="margin-bottom: 20px">
          <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
            <el-form-item label="请假类型" prop="type">
              <el-select v-model="queryParams.type" placeholder="请选择请假类型" clearable size="small">
                <el-option
                  v-for="dict in typeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="标题" prop="title">
              <el-input
                v-model="queryParams.title"
                placeholder="请输入标题"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
        <el-card>
          <el-row :gutter="10" class="mb8">
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>

          <el-table v-loading="loading" :data="leaveApplicationList" @current-change="handleCurrentChange"
                    height="calc(100vh - 312px)" highlight-current-row @row-dblclick="rowDblClick($event)">
            <el-table-column label="请假编号" align="center" prop="id" width="100"/>
            <el-table-column label="请假类型" align="center" prop="type" :formatter="typeFormat"/>
            <el-table-column label="标题" align="left" prop="title" min-width="150"/>
            <el-table-column label="原因" align="left" prop="reason" min-width="250"/>
            <el-table-column label="开始时间" align="center" prop="leaveStartTime" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.leaveStartTime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="结束时间" align="center" prop="leaveEndTime" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.leaveEndTime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="备注" align="center" prop="remark"/>
            <el-table-column align="center" class-name="small-padding fixed-width" fixed="right" label="操作" width="220">
              <template slot-scope="scope">
                <claim-button :row="scope.row" @approval="handleApproval(scope.row)" @refreshDataList="getList"></claim-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-card>
      </el-tab-pane>
      <el-tab-pane label="已办列表" name="second">
        <el-card v-show="showSearch" style="margin-bottom: 20px">
          <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
            <el-form-item label="请假类型" prop="type">
              <el-select v-model="queryParams.type" placeholder="请选择请假类型" clearable size="small">
                <el-option
                  v-for="dict in typeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="标题" prop="title">
              <el-input
                v-model="queryParams.title"
                placeholder="请输入标题"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
        <el-card>
          <el-row :gutter="10" class="mb8">
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>
          <el-table v-loading="loading" :data="leaveApplicationApprovalHistoryList" @current-change="handleCurrentChange"
                    height="calc(100vh - 312px)" highlight-current-row @row-dblclick="rowDblClick($event)">
            <el-table-column label="请假编号" align="center" prop="id" width="100"/>
            <el-table-column label="请假类型" align="center" prop="type" :formatter="typeFormat"/>
            <el-table-column label="标题" align="left" prop="title" min-width="150"/>
            <el-table-column label="原因" align="left" prop="reason" min-width="100"/>
            <el-table-column label="开始时间" align="center" prop="leaveStartTime" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.leaveStartTime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="结束时间" align="center" prop="leaveEndTime" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.leaveEndTime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="historyDataDTO.formHistoryDataDTO" label="执行结果">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.historyDataDTO.formHistoryDataDTO[0].value === '同意'"
                  disable-transitions
                  type="success">同意
                </el-tag>
                <el-tag v-else-if="scope.row.historyDataDTO.formHistoryDataDTO[0].value === '不同意'"
                  disable-transitions
                  type="danger">驳回
                </el-tag>
                <el-tag v-else-if="scope.row.historyDataDTO.formHistoryDataDTO[0].value === '已调整'"
                  disable-transitions
                  type="primary">已调整
                </el-tag>
                <el-tag v-else
                  disable-transitions
                  type="primary">已调整
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column
              prop="historyDataDTO.createdDate"
              label="操作时间">
            </el-table-column>
            <el-table-column
              prop="historyDataDTO.formHistoryDataDTO"
              label="审批意见">
              <template slot-scope="scope">
                <span>{{ scope.row.historyDataDTO.formHistoryDataDTO[1].value }}</span>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-card>
      </el-tab-pane>
    </el-tabs>
    <edit ref="editDialog" @refreshDataList="getList"></edit>
    <detail ref="detailDialog" @refreshDataList="getList"></detail>
  </div>
</template>

<script>
  import { applyList, approvalRecordList } from '@/api/example/leaveappr/base'
  import StatusValueToText from '@/components/StatusValueToText'
  import Edit from './edit'
  import Detail from '../base/detail'
  import ClaimButton from '@/components/ClaimButton'

  export default {
    name: 'LeaveApplicationApproval',
    components: { Edit, Detail, StatusValueToText, ClaimButton, },
    data() {
      return {
        // 激活tab
        activeName: 'first',
        // 遮罩层
        loading: true,
        // 选中数组
        id: '',
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: false,
        // 总条数
        total: 0,
        // 请假申请表格数据
        leaveApplicationList: [],
        // 请假审批历史表格数据
        leaveApplicationApprovalHistoryList: [],
        // 请假类型字典
        typeOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          type: null,
          title: null,
          reason: null,
          leaveStartTime: null,
          leaveEndTime: null,
          instanceId: null,
          state: null
        }
      }

    },
    created() {
      this.getList()
      this.getDicts('sys_notice_type').then(response => {
        this.typeOptions = response.data
      })
    },
    watch: {
      // 监听如果tab页面变化调用对应页面的查询
      activeName(val) {
        if (val === 'first') {
          this.getList()
        } else if (val === 'second') {
          this.getApprovalList()
        }
      }
    },
    methods: {
      /** 查询请假申请列表 */
      getList() {
        this.loading = true
        // 待办
        applyList(this.queryParams).then(response => {
          this.leaveApplicationList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      /** 查询请假申请办理历史列表 */
      getApprovalList() {
        this.loading = true
        // 待办历史
        approvalRecordList(this.queryParams).then(response => {
          console.log('leaveApplicationApprovalHistoryList res', response)
          this.leaveApplicationApprovalHistoryList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 请假类型字典翻译
      typeFormat(row, column) {
        return this.selectDictLabel(this.typeOptions, row.type)
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      //选中数据
      handleCurrentChange(val) {
        if (val) {
          this.id = val.id
          if (this.id) {
            this.single = false
          }
        }
      },
      /** 双击查看 */
      rowDblClick(row) {
        this.handleDetail(row)
      },
      /** 审核按钮操作 */
      handleApproval(row) {
        this.$refs['editDialog'].reset()
        this.$refs['editDialog'].open = true
        const id = row.id || this.id
        this.$refs['editDialog'].init(id)
      },
      /** 查看按钮操作 */
      handleDetail(row) {
        this.$refs['detailDialog'].reset()
        this.$refs['detailDialog'].open = true
        const id = row.id || this.id
        this.$refs['detailDialog'].init(id)
      },
      handleTabClick(tab, event) {
        this.activeName = tab.name
      }
    }
  }
</script>
